package com.fc.test.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.DateUtil;
import com.fc.test.util.EmojiUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;

/**
 * Service
* @Title: TAbbMemberService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-22 16:39:57  
 */
@Service
public class TAbbMemberService implements BaseService<TAbbMember, TAbbMemberExample>{
	@Autowired
	private TAbbMemberMapper tAbbMemberMapper;

	@Autowired
	private TAbbCommissionMapper commissionMapper;

	@Autowired
	private TAbbWithdrawMapper withdrawMapper;

	@Autowired
	private TAbbOrderMapper orderMapper;

	@Autowired
	private TAbbRenzhengMapper renzhengMapper;

	@Autowired
	private TAbbScanService scanService;


	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @param searchTxt
     * @param address
     * @param isvip
     * @param starttime
     * @return
	 */
	 public PageInfo<TAbbMember> list(Tablepar tablepar, String name, String address, String isvip, String starttime, String endtime){
	        TAbbMemberExample testExample=new TAbbMemberExample();
	        testExample.setOrderByClause("id ASC");
		   TAbbMemberExample.Criteria criteria= testExample.createCriteria();
	        if(name!=null&&!"".equals(name)){
				criteria.andNicenameLike("%"+name+"%");
	        }
			 if(address!=null&&!"".equals(address)){
				 criteria.andAddressLike("%"+address+"%");
			 }
			 if(isvip!=null&&!"".equals(isvip)&&!isvip.equals("-1")){
				 criteria.andIsvipEqualTo(Integer.parseInt(isvip));
			 }
			 if(starttime!=null&&!"".equals(starttime)){
				 criteria.andYuliu1GreaterThan(starttime);
			 }
			 if(endtime!=null&&!"".equals(endtime)){
				 criteria.andYuliu1LessThanOrEqualTo(endtime);
			 }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TAbbMember> list= tAbbMemberMapper.selectByExample(testExample);
	        PageInfo<TAbbMember> pageInfo = new PageInfo<TAbbMember>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		TAbbMemberExample example=new TAbbMemberExample();
		example.createCriteria().andIdIn(lista);
		return tAbbMemberMapper.deleteByExample(example);
	}


	@Override
	public TAbbMember selectByPrimaryKey(String id) {

		return tAbbMemberMapper.selectByPrimaryKey(id);
	}


	@Override
	public int updateByPrimaryKeySelective(TAbbMember record) {
		return tAbbMemberMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbMember record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbMemberMapper.insertSelective(record);
	}










	@Override
	public int updateByExampleSelective(TAbbMember record, TAbbMemberExample example) {

		return tAbbMemberMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbMember record, TAbbMemberExample example) {

		return tAbbMemberMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbMember> selectByExample(TAbbMemberExample example) {

		return tAbbMemberMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbMemberExample example) {

		return tAbbMemberMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbMemberExample example) {

		return tAbbMemberMapper.deleteByExample(example);
	}


	public Map<String,Object> registerMember(String wxOpenId,String nicename,String wximages,String yqrId){
		Map<String,Object> resMap = new HashMap<>();
		TAbbScan scan = scanService.getSacnNum();
		if(null!=scan){
			scan.setTotalnum(scan.getTotalnum()+1);
			scanService.updateByPrimaryKeySelective(scan);
		}
		if(wxOpenId!=null&&!wxOpenId.equals("")){
			TAbbMember member = (TAbbMember) tAbbMemberMapper.selectByWxopenid(wxOpenId);
			if (member != null) {
				resMap.put("success", true);
				resMap.put("msg", "登录成功！");
				resMap.put("flag","2");
				resMap.put("member", member);
				return resMap;
			}
		}

		//处理带表情的昵称
		nicename = EmojiUtil.filterEmoji(nicename);

		TAbbMember obj = new TAbbMember();
		obj.setId(SnowflakeIdWorker.getUUID());
		obj.setNicename(nicename);
		obj.setWximages(wximages);
		obj.setOpenid(wxOpenId);
		obj.setYqrid(yqrId);
		obj.setYuliu1(DateUtil.getCurrentDateTimeStr());
		obj.setAccount(0d);
		obj.setIsvip(0);
		obj.setZhidingnum(0);
		obj.setIsshop(0);
		obj.setYuliu3("0");
		tAbbMemberMapper.insert(obj);

		resMap.put("success", true);
		resMap.put("msg", "注册成功！");
		resMap.put("flag","1");
		resMap.put("member", obj);
		return resMap;
	}

	public int updateMemberInfo(String id,String phone,String qq,String address){
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(id);
		member.setPhone(phone);
		member.setQq(qq);
		member.setAddress(address);
		int flag = tAbbMemberMapper.updateByPrimaryKey(member);
		return flag;
	}

	public List<?> getFundRecords(String memberid,int type,int page){
		List<?> list = null;
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("memberid",memberid);
		paramMap.put("page",page);
		if(type==1){
			list = commissionMapper.getRecordList(paramMap);
		}else if(type==2){
			list = withdrawMapper.getRecordList(paramMap);
		}else if(type==3){
			list = orderMapper.getRecordList(paramMap);
		}
        return list;
	}


	public int applyRenZheng(TAbbRenzheng renzheng){
		renzheng.setId(SnowflakeIdWorker.getUUID());
		renzheng.setCreattime(DateUtil.getCurrentDateTimeStr());
		renzheng.setStatus(0);

		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(renzheng.getUserid());
		if(null!=member){
			renzheng.setUsername(member.getNicename());
		}

		int flag = renzhengMapper.insert(renzheng);
		return flag;
	}

	public List<TAbbMember> getMyTeam(String userid,int page){
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("userid",userid);
		paramMap.put("page",page);
		List<TAbbMember> list = tAbbMemberMapper.getMyTeam(paramMap);
		return list;
	}

}
